//
//  KMHeaderTableListController.m
//  HeaderTableViewListController
//
//  Created by 陈家将 on 16/7/28.
//  Copyright © 2016年 kimeeos. All rights reserved.
//

#import "KMHeaderTableListController.h"
#import "KMHeaderView.h"
#import "KMSegmentView.h"

@interface KMHeaderTableListController ()<UIScrollViewDelegate>


@end

@implementation KMHeaderTableListController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customInit];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    UITableViewController *tabVc = _km_viewControllers[_km_selectedIndex];
    [tabVc.view removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)customInit {
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 容器
    [self.view addSubview:self.km_containerView];
    self.km_containerView.backgroundColor = COLOR_RED;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    CGPoint point = [change[@"new"] CGPointValue];
    
    if (point.y > -_km_segmentHeight) { // 上滚悬停判断
        point.y = -_km_segmentHeight;
    }
    if (point.y < -(_km_headerHeight + _km_segmentHeight)) { // 下滚悬停判断
        point.y = -(_km_headerHeight + _km_segmentHeight);
    }
    
    self.km_headerView.frame = CGRectMake(0,-(_km_headerHeight + _km_segmentHeight + point.y), _km_headerView.bounds.size.width, _km_headerView.bounds.size.height);
    self.km_segmentView.frame = CGRectMake(0, -(_km_segmentHeight + point.y), SCREEN_WIDTH, _km_segmentHeight);
    
    // 动态改变所有子控制器的contenInsert
    [_km_containerView.subviews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UITableView class]]) {
            UITableView *tabV = (UITableView *)obj;
            if (![object isEqual:tabV]) {
                if (tabV.contentOffset.y < -_km_segmentHeight || point.y != -_km_segmentHeight) { // 需要重新计算其他tableview位移的逻辑
                    tabV.contentOffset = CGPointMake(0, point.y);
                }
            }
        }
    }];
}

- (void)setKm_viewControllers:(NSArray *)km_viewControllers {
    self.km_containerView.contentSize = CGSizeMake(SCREEN_WIDTH * km_viewControllers.count, 0);
    _km_viewControllers = km_viewControllers;
    [km_viewControllers enumerateObjectsUsingBlock:^(UITableViewController *vc, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addChildViewController:vc];
        [vc.view willMoveToSuperview:self.view];
        vc.view.frame = CGRectMake(idx * _km_containerView.bounds.size.width, 0, _km_containerView.bounds.size.width, _km_containerView.bounds.size.height);
        [_km_containerView addSubview:vc.view];
        [vc.view didMoveToSuperview];
        
        vc.tableView.contentInset = UIEdgeInsetsMake(_km_headerHeight+_km_segmentHeight, 0, 0, 0);
        if (idx == _km_selectedIndex) {
            [vc.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        }
    }];
}

- (void)setKm_selectedIndex:(NSInteger)km_selectedIndex {
    UITableViewController *selecteVc = _km_viewControllers[_km_selectedIndex];
    [selecteVc.view removeObserver:self forKeyPath:@"contentOffset"];
    _km_selectedIndex = km_selectedIndex;
    
    UITableViewController *nextSelectedVc = _km_viewControllers[_km_selectedIndex];
    [nextSelectedVc.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)setKm_headerView:(KMHeaderView *)km_headerView {
    _km_headerView = km_headerView;
    [self.view addSubview:km_headerView];
}

- (void)setKm_headerHeight:(CGFloat)km_headerHeight {
    _km_headerHeight = km_headerHeight;
    self.km_headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, km_headerHeight);
}

- (void)setKm_segmentView:(KMSegmentView *)km_segmentView {
    _km_segmentView = km_segmentView;
    [self.view addSubview:km_segmentView];
}

- (void)setKm_segmentHeight:(CGFloat)km_segmentHeight {
    _km_segmentHeight = km_segmentHeight;
    self.km_segmentView.frame = CGRectMake(0, CGRectGetMaxY(_km_headerView.frame), SCREEN_WIDTH, km_segmentHeight);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int page = (int)(scrollView.contentOffset.x/SCREEN_WIDTH);
    self.km_selectedIndex = page;
    
    UITableViewController *currentVc = _km_viewControllers[page];
    if (currentVc.tableView.contentSize.height < currentVc.tableView.bounds.size.height) {
        [currentVc.tableView setContentOffset:CGPointMake(0, -currentVc.tableView.contentInset.top) animated:YES];
    }
}

- (void)KMSegmentViewDelegate:(KMSegmentView*)menuciew WithIndex:(int)index {
    [self.km_containerView setContentOffset:CGPointMake(index * SCREEN_WIDTH, 0) animated:NO];
    self.km_selectedIndex = index;
}

- (void)MenuViewDelegate:(KMSegmentView *)menuciew WithIndex:(int)index {
    self.km_containerView.contentOffset = CGPointMake(index * SCREEN_WIDTH, 0);
    self.km_selectedIndex = index;
}

- (UIScrollView *)km_containerView {
    if (_km_containerView == nil) {
        _km_containerView = [[UIScrollView alloc] initWithFrame:SCREEN_BOUNDS_WITHOUT_NAV_AND_TABBAR];
        _km_containerView.showsVerticalScrollIndicator = NO;
        _km_containerView.showsHorizontalScrollIndicator = NO;
        _km_containerView.pagingEnabled = YES;
        _km_containerView.bounces = NO;
        _km_containerView.delegate = self;
    }
    
    return _km_containerView;
}

- (void)removeKVO {
    UITableViewController *tabVc = _km_viewControllers[_km_selectedIndex];
    [tabVc.view removeObserver:self forKeyPath:@"contentOffset"];
}

@end
